<?php
include '../admin_head.php';
//include '../common.php';
//按条件查找（获取分类信息）
$sql = "select id,name,path,concat(path,id) bpath from ".PRE."category order by bpath";
	$result = mysql_query($sql);
	if($result && mysql_num_rows($result)>0){
		while($row = mysql_fetch_assoc($result)){
			$row['name'] = str_repeat('&nbsp;&nbsp;',(strlen($row['path'])-2)/2).$row['name'];
			$catelist[] = $row;
		}
	}

//处理搜索分页数据
	$category = $_GET['cg'];
	$style = $_GET['st'];
	
	if($category != null) $search[] = "(cate_id in (select id from ".PRE."category where path like '%,{$category},%') || cate_id = '{$category}')";
	if($style != null) $search[] = "style='{$style}'";
	
	if($search != null ) $where = 'where '. implode(' and ',$search);
	//重点理解三种情况的where的取值
	//$where = empty($_GET['where']) ? $where : stripslashes($_GET['where']);



//获取总记录数
	$sql = "select count(id) num from ". PRE ."goods {$where}" ;
    $result = mysql_query($sql);
    
    if($result && mysql_affected_rows() > 0){
        $row = mysql_fetch_assoc($result);
    }

    $total = $row['num'];       //总记录数
    //每页显示数
    $num = 5;

    //总页数
    $amount = ceil($total / $num);

    //页码
    $page = empty($_GET['p'])? 1 : (int)$_GET['p'];

    if($page < 1){
        $page = 1;
    }

    if($page > $amount){
        $page = $amount;
    }

    //偏移量
    $offset = ($page - 1) * $num;  



$sql = "select id,name,image,price,store,is_up,status,addtime from ".PRE."goods {$where} order by id limit $offset,$num";
$result = mysql_query($sql);
if($result && mysql_num_rows($result) > 0){
	while($row = mysql_fetch_assoc($result)){
		$goodslist[] = $row;
	}
}
//制作分页按钮
 $start = ($page - 3) <= 1 ? 1 : $page - 3;

    $end = ($page + 3) >= $amount ? $amount : $page + 3 ;

    for($i = $start; $i <= $end; $i++){
        if($i == $page){
            $str .= '<li><a style="color:#E50065;font-weight:bold;" href="index.php?cg='.$category.'&st='.$style.'&p='. $i .'" >['. $i .']</a></li>';
            continue;
        }
            $str .= '<li><a href="index.php?cg='.$category.'&st='.$style.'&p='. $i .'">['. $i .']</a><li>';

    }


    $nav = <<<tag
        <li><a href="index.php?cg={$category}&st={$style}&p=1">首页</a></li>
        $str
        <li><a href="index.php?cg={$category}&st={$style}&p=$amount">尾页</a></li>
tag;



?>

	<div class="admin_main_nav">商品管理&gt;&gt;商品列表</div>
	<div id="goods_index" class="admin_main_index">
		<div class="width">
			<div class="user_index_title">
				<div class="user_search fr">
					<form action="index.php" method="get">
						类别：
							<select name="cg">
								<option value="">全部商品</option>
								<?php foreach($catelist as $tr):?>
								<option value="<?php echo $tr['id'] ?>"><?php echo $tr['name'] ?></option>
								<?php endforeach;?>
							</select>
						风格：
							<select name="st">
								<option value="">所有风格</option>
								<option value="0">普通风格</option>
								<option value="1">时尚内衣</option>
								<option value="2">优雅女装</option>
								<option value="3">休闲女装</option>
								<option value="4">日韩女装</option>
								<option value="5">欧式女装</option>
								<option value="6">意式女装</option>
								<option value="7">年轻潮牌</option>
								<option value="8">高端男装</option>
							</select>
								<input type="submit" value="搜索" />
					</form>
				</div>	
			</div>
			<div>
				<table width="800" align="center">
					<tr>
						<th>商品ID</th>
						<th>图片</th>
						<th>价格</th>
						<th>库存</th>
						<th>是否上架</th>
						<th>是否热销</th>
						<th>是否新品</th>
						<th>是否精品</th>
						<th>添加时间</th>
						<th width="160">操作</th>
					</tr>
					<?php if(!empty($goodslist)):?>
					<?php foreach($goodslist as $tr):?>
					<tr>
						<td><?php echo $tr['id'] ?></td>
						<td><a href="<?php echo APP.'/detail.php?gid='.$tr['id']?>" target="_top"><img src="<?php echo '../../upload/'.$tr['image'] ?>" title="<?php echo $tr['name']?>" height="50"/></a></td>
						<td><?php echo $tr['price'] ?></td>
						<td><?php echo $tr['store'] ?></td>
						<td><?php 
							if($tr['is_up'] == 0) echo '<a href="action.php?a=dis&cg='.$category.'&st='.$style.'&page='.$page.'&dis=1&id='.$tr['id'].'"><img src="../../images/no.gif" /></a>';
							if($tr['is_up'] == 1) echo '<a href="action.php?a=dis&cg='.$category.'&st='.$style.'&page='.$page.'&dis=0&id='.$tr['id'].'"><img src="../../images/yes.gif" /></a>';

						?></td>
						<td><?php 
							if($tr['status'] == 1 || $tr['status'] == 3 || $tr['status'] == 5 || $tr['status'] == 7){
							$status = $tr['status'] - 1;
							echo '<a href="action.php?a=status&id='.$tr['id'].'&cg='.$category.'&st='.$style.'&page='.$page.'&status='.$status.'"><img src="../../images/yes.gif" /></a>';
							}else{
							$status = $tr['status'] + 1;
							echo '<a href="action.php?a=status&id='.$tr['id'].'&cg='.$category.'&st='.$style.'&page='.$page.'&status='.$status.'"><img src="../../images/no.gif" /></a>';
							}
						?></td>
						<td><?php 
							if($tr['status'] == 2 || $tr['status'] == 3 || $tr['status'] == 6 || $tr['status'] == 7){
							$status = $tr['status'] - 2 ;
							echo '<a href="action.php?a=status&id='.$tr['id'].'&cg='.$category.'&st='.$style.'&page='.$page.'&status='.$status.'"><img src="../../images/yes.gif" /></a>';
							}else{
							$status = $tr['status'] + 2;
							echo '<a href="action.php?a=status&id='.$tr['id'].'&cg='.$category.'&st='.$style.'&page='.$page.'&status='.$status.'"><img src="../../images/no.gif" /></a>';
							}
						?></td>
						<td><?php 
							if($tr['status'] == 4 || $tr['status'] == 5 || $tr['status'] == 6 || $tr['status'] == 7){
							$status = $tr['status'] - 4;
							echo '<a href="action.php?a=status&id='.$tr['id'].'&cg='.$category.'&st='.$style.'&page='.$page.'&status='.$status.'"><img src="../../images/yes.gif" /></a>';
							}else{
							$status = $tr['status'] + 4;
							echo '<a href="action.php?a=status&id='.$tr['id'].'&cg='.$category.'&st='.$style.'&page='.$page.'&status='.$status.'"><img src="../../images/no.gif" /></a>';
							}
						?></td>
						<td><?php echo date('Y-m-d',$tr['addtime']) ?></td>
						<td>
							<div class="action">
								<div class="fl"><a class="edit" href="edit.php?cg=<?php echo $category?>&st=<?php echo $style?>&p=<?php echo $page?>&id=<?php echo $tr[id] ?>"><div class="icon fl edit_i"></div><div class="fl">编辑</div></a></div>
								<div class="fl"><a class="del" href="action.php?a=del&id=<?php echo $tr[id] ?>"><div class="icon fl del_i"></div><div class="fl">删除</div></a></div>
							</div>
						</td>
					</tr>
					<?php endforeach;?>
					<?php else:?>
						<tr>
							<td colspan="10">没有找到任何记录</td>
						</tr>
					<?php endif;?>
				</table>
			</div>
			<!-- 分页按钮 -->
			<div class="nav">
				<div class="tips"><?php $start = $offset + 1>0? $offset+1: 0;$end = $offset + $num >= $total? $total : $offset + $num; echo "Showing $start to $end of $total entries";?></div>
				<ul class="user_nav">
					<?php echo $nav ?>
				</ul>
			</div>
		</div>
	</div>

	
<?php include '../admin_footer.php'; ?>

